import request from '@/network'
import { UserInfo, Password, Profile, UserDetail } from './types'

export const loginRequest = async (userInfo: UserInfo) => {
  const result = await request.post({
    url: '/user/login',
    data: userInfo,
  })
  return result
}

export const updatePwdRequest = async (password: Password) => {
  const result = await request.patch({
    url: '/user/pwd',
    data: {
      oldPwd: password.old,
      newPwd: password.new,
      confirmPwd: password.confirm,
    },
  })
  return result
}

export const updateProfileRequest = async (profile: Profile) => {
  const result = await request.patch({
    url: '/user/profile',
    data: {
      nickname: profile.nickname,
      desc: profile.desc,
      website: profile.website,
    },
  })
  return result
}

export const getUserProfileRequest = async (id: number) => {
  const result = await request.get({
    url: `/user/profile/${id}`,
  })
  return result
}

export const uploadUserAvatarRequest = async (formdata: FormData) => {
  const result = await request.post({
    url: '/upload/avatar',
    data: formdata,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return result
}

export const getUsersListRequest = async (currentPage: number = 1, pageSize: number = 7) => {
  const result = await request.get({
    url: '/user/list',
    params: {
      currentPage,
      pageSize,
    },
  })
  return result
}

export const getVisitorListRequest = async (currentPage: number = 1, pageSize: number = 7) => {
  const result = await request.get({
    url: '/user/visitors',
    params: {
      currentPage,
      pageSize,
    },
  })
  return result
}

export const assignAvatar2UserRequest = async (formdata: FormData) => {
  const result = await request.post({
    url: '/user/assign/avatar',
    data: formdata,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return result
}

export const deleteUserRequest = async (user_id: number) => {
  const result = await request.delete({
    url: '/user/delete',
    data: {
      user_id,
    },
  })
  return result
}

export const updateUserInfoRequest = async (userInfo: UserDetail) => {
  const result = await request.patch({
    url: '/user/assign',
    data: {
      userInfo,
    },
  })
  return result
}
